//
//  MarketKpiInfo.m
//  AppFinaceHT
//
//  Created by LionGlory on 13-9-1.
//  Copyright (c) 2013年 Lion. All rights reserved.
//

#import "MarketKpiInfo.h"

static MarketKpiInfo *kpiInfo;

@implementation MarketKpiInfo

+ (MarketKpiInfo*) manager{
    if(kpiInfo == nil){
        kpiInfo = [[self alloc] init];
    }
    
    return kpiInfo;
}

// 设置 kpi 最后一条需要显示的数据
- (void)kpiInfoDataRefreshWithKpiType:(int)kpiType{
    
    [kpiLab1 removeFromSuperview];
    [kpiLab2 removeFromSuperview];
    [kpiLab3 removeFromSuperview];
    [pointLab1 removeFromSuperview];
    [pointLab2 removeFromSuperview];
    [pointLab3 removeFromSuperview];
    
    
    kpiLab1 = [[UILabel alloc] init];
    kpiLab2 = [[UILabel alloc] init];
    kpiLab3 = [[UILabel alloc] init];
    pointLab1 = [[UILabel alloc] init];
    pointLab2 = [[UILabel alloc] init];
    pointLab3 = [[UILabel alloc] init];
    kpiLab1.adjustsFontSizeToFitWidth = YES;
    kpiLab1.minimumScaleFactor = 0.5;
    kpiLab2.adjustsFontSizeToFitWidth = YES;
    kpiLab2.minimumScaleFactor = 0.5;
    kpiLab3.adjustsFontSizeToFitWidth = YES;
    kpiLab3.minimumScaleFactor = 0.5;
    
    
    UIColor *fontColor = [AFUtils UIColorFromRGB:0x949494 alpha:1.0];
//    if(![[NSUserDefaults standardUserDefaults] boolForKey:DayMode]){
//        fontColor = K_APP_LIST_FONT_BLACKCOLOR;
//    }
    // 0: MACD平滑异同平均线
    // 1: VOL成交量
    // 2: RSI强弱指标
    // 3: BOLL布林线
    // 4: KDJ随机指标
    // 5: OBV能量潮
    // 6: CCI顺势
    // 7: PSY心里线
    if(kpiType == 0){  // MACD
        float _difLast = [self.kpi macdLastDif];
        float _macdLast = [self.kpi macdLastMacd];
        float _dmLast = [self.kpi macdLastDm];
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab1.frame = CGRectMake(5, 0, 75, 17);
           [kpiLab1 setFont:[UIFont systemFontOfSize:10]];
            kpiLab1.adjustsFontSizeToFitWidth = YES;
        }else{
           kpiLab1.frame = CGRectMake(5, 0, 105, 17);
           [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        }
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"DIF: %0.4f",_difLast]];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        [kpiLab1 setTextColor:fontColor];
        
        
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0xffae00 alpha:1.0]];
        
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab2.frame =  CGRectMake(93, 0, 75, 17);
            [kpiLab2 setFont:[UIFont systemFontOfSize:10]];
            kpiLab2.adjustsFontSizeToFitWidth = YES;
            pointLab2.frame = CGRectMake(88, 6, 4, 4);
        }else{
             kpiLab2.frame =  CGRectMake(115, 0, 105, 17);
            [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
            pointLab2.frame = CGRectMake(110, 6, 4, 4);
        }
        [kpiLab2 setBackgroundColor:[UIColor clearColor]];
        [kpiLab2 setText:[NSString stringWithFormat:@"DEA: %0.4f",_macdLast]];
        
        [kpiLab2 setTextColor:fontColor];
        kpiLab2.textAlignment = NSTextAlignmentLeft;
        
        
        [pointLab2 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];

        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab3.frame = CGRectMake(177, 0, 75, 17);
            [kpiLab3 setFont:[UIFont systemFontOfSize:10]];
            kpiLab3.adjustsFontSizeToFitWidth = YES;
            pointLab3.frame = CGRectMake(172, 6, 4, 4);
            
        }else{
              kpiLab3.frame = CGRectMake(225, 0, 105, 17);
              [kpiLab3 setFont:[UIFont systemFontOfSize:11]];
              pointLab3.frame = CGRectMake(220, 6, 4, 4);
        }
        [kpiLab3 setBackgroundColor:[UIColor clearColor]];
        [kpiLab3 setText:[NSString stringWithFormat:@"MACD: %0.4f",_dmLast]];
        [kpiLab3 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab3 setTextColor:fontColor];
        kpiLab3.textAlignment = NSTextAlignmentLeft;
        
        
        [pointLab3 setBackgroundColor:[AFUtils UIColorFromRGB:0xf847bd alpha:1.0]];
        
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        [pointLab2.layer setMasksToBounds:YES];
        [pointLab2.layer setCornerRadius:2.0];
        [pointLab3.layer setMasksToBounds:YES];
        [pointLab3.layer setCornerRadius:2.0];

        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
        [self.kpiInfoView addSubview:kpiLab2];
        [self.kpiInfoView addSubview:pointLab2];
        [self.kpiInfoView addSubview:kpiLab3];
        [self.kpiInfoView addSubview:pointLab3];
        
    }
    // VOL
    else if(kpiType == 1){
        
        float _vol = [self.kpi volLastVol];
        float _vol_m5 = [self.kpi volLastM5];
        float _vol_m10 = [self.kpi volLastM10];
        
        kpiLab1.frame = CGRectMake(0, 0, 100, 17);
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        float _v = _vol/10000;
        if (_v > 1.0) {
            [kpiLab1 setText:[NSString stringWithFormat:@"VOL: %.0f万",_v]];
        } else {
            [kpiLab1 setText:[NSString stringWithFormat:@"VOL: %.2f万",_v]];
        }
        [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
//        [kpiLab1 setTextColor:[UIColor whiteColor]];
        [kpiLab1 setTextColor:[AFUtils UIColorFromRGB:0xFDA01C alpha:1.0]];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        
        kpiLab2.frame = CGRectMake(100, 0, 100, 17);
        [kpiLab2 setBackgroundColor:[UIColor clearColor]];
        float _m5 = _vol_m5/10000;
        if (_m5 > 1.0) {
            [kpiLab2 setText:[NSString stringWithFormat:@"5: %.0f万",_m5]];
        } else {
            [kpiLab2 setText:[NSString stringWithFormat:@"5: %.2f万",_m5]];
        }
        [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
//        [kpiLab2 setTextColor:[UIColor yellowColor]];
        [kpiLab2 setTextColor:[AFUtils UIColorFromRGB:0x24ACEE alpha:1.0]];
        kpiLab2.textAlignment = NSTextAlignmentLeft;
        
        kpiLab3.frame = CGRectMake(200, 0, 100, 17);
        [kpiLab3 setBackgroundColor:[UIColor clearColor]];
        float _m10 = _vol_m10/10000;
        if (_m10 > 1.0) {
            [kpiLab3 setText:[NSString stringWithFormat:@"10: %.0f万",_m10]];
        } else {
            [kpiLab3 setText:[NSString stringWithFormat:@"10: %.2f万",_m10]];
        }
        [kpiLab3 setFont:[UIFont systemFontOfSize:11]];
//        [kpiLab3 setTextColor:[UIColor colorWithRed:1.0 green:0 blue:1.0 alpha:1.0]];
         [kpiLab3 setTextColor:[UIColor magentaColor]];
        kpiLab3.textAlignment = NSTextAlignmentLeft;
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:kpiLab2];
        [self.kpiInfoView addSubview:kpiLab3];
    }
    // RSI
    else if(kpiType == 2){  // RSI
        
        float _rsiM6 = [self.kpi rsiLastM6];
        float _rsiM12 = [self.kpi rsiLastM12];
        
        kpiLab1.frame = CGRectMake(5, 0, 95, 17);
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"6: %.2f",_rsiM6]];
        [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0xffae00 alpha:1.0]];

        kpiLab2 = [[UILabel alloc] initWithFrame:CGRectMake(105, 0, 95, 17)];
        [kpiLab2 setBackgroundColor:[UIColor clearColor]];
        [kpiLab2 setText:[NSString stringWithFormat:@"12: %.2f",_rsiM12]];
        [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab2 setTextColor:fontColor];
        kpiLab2.textAlignment = NSTextAlignmentLeft;
        pointLab2.frame = CGRectMake(100, 6, 4, 4);
        [pointLab2 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];
  
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        [pointLab2.layer setMasksToBounds:YES];
        [pointLab2.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
        [self.kpiInfoView addSubview:kpiLab2];
        [self.kpiInfoView addSubview:pointLab2];
        
        
    }
    // BOLL
    else if(kpiType == 3){  // 3: BOLL布林线
        float _lasetAverage = [self.kpi boolLastMid];
        float _lasetUp = [self.kpi boolLastUp];
        float _lasetDown = [self.kpi boolLastDown];
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab1.frame = CGRectMake(5, 0, 75, 17);
            [kpiLab1 setFont:[UIFont systemFontOfSize:10]];
            kpiLab1.adjustsFontSizeToFitWidth = YES;
        }else{
              kpiLab1.frame = CGRectMake(5, 0, 95, 17);
             [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        }
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"MID: %0.0f",_lasetAverage]];
       
        [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0xffae00 alpha:1.0]];
        
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab2.frame = CGRectMake(93, 0, 75, 17);
            [kpiLab2 setFont:[UIFont systemFontOfSize:10]];
            kpiLab2.adjustsFontSizeToFitWidth = YES;
            pointLab2.frame = CGRectMake(88, 6, 4, 4);
        }else{
        kpiLab2.frame = CGRectMake(105, 0, 95, 17);
        [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
        pointLab2.frame = CGRectMake(100, 6, 4, 4);
        }
        [kpiLab2 setBackgroundColor:[UIColor clearColor]];
        [kpiLab2 setText:[NSString stringWithFormat:@"UPPER: %0.0f",_lasetUp]];
        
        [kpiLab2 setTextColor:fontColor];
        kpiLab2.textAlignment = NSTextAlignmentLeft;
        
        
        [pointLab2 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];
        
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab3.frame = CGRectMake(177, 0, 75, 17);
            [kpiLab3 setFont:[UIFont systemFontOfSize:10]];
            pointLab3.frame = CGRectMake(172, 6, 4, 4);
        }else{
            kpiLab3.frame = CGRectMake(205, 0, 95, 17);
            [kpiLab3 setFont:[UIFont systemFontOfSize:11]];
            pointLab3.frame = CGRectMake(200, 6, 4, 4);
        }
        
        [kpiLab3 setBackgroundColor:[UIColor clearColor]];
        [kpiLab3 setText:[NSString stringWithFormat:@"LOWER: %0.0f",_lasetDown]];
        
        [kpiLab3 setTextColor:fontColor];
        kpiLab3.textAlignment = NSTextAlignmentLeft;
        
        [pointLab3 setBackgroundColor:[AFUtils UIColorFromRGB:0xf847bd alpha:1.0]];
        
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        [pointLab2.layer setMasksToBounds:YES];
        [pointLab2.layer setCornerRadius:2.0];
        [pointLab3.layer setMasksToBounds:YES];
        [pointLab3.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
        [self.kpiInfoView addSubview:kpiLab2];
        [self.kpiInfoView addSubview:pointLab2];
        [self.kpiInfoView addSubview:kpiLab3];
        [self.kpiInfoView addSubview:pointLab3];
        
        


        
    }
    // 4: KDJ随机指标n
    else if(kpiType == 4){
        float _lasetK= [self.kpi kdjLastK];
        float _lasetD = [self.kpi kdjLastD];
        float _lasetJ = [self.kpi kdjLastJ];
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab1.frame = CGRectMake(5, 0, 75, 17);
            [kpiLab1 setFont:[UIFont systemFontOfSize:10]];
            kpiLab1.adjustsFontSizeToFitWidth = YES;
        }else{
            kpiLab1.frame = CGRectMake(5, 0, 95, 17);
            [kpiLab1 setFont:[UIFont systemFontOfSize:11]];

        }
        
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"K: %.2f",_lasetK]];
                [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0xffae00 alpha:1.0]];
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab2.frame = CGRectMake(93, 0, 75, 17);
            [kpiLab2 setFont:[UIFont systemFontOfSize:10]];
            kpiLab2.adjustsFontSizeToFitWidth = YES;
            pointLab2.frame = CGRectMake(88, 6, 4, 4);
        }else{
            kpiLab2.frame = CGRectMake(105, 0, 95, 17);
            [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
             pointLab2.frame = CGRectMake(100, 6, 4, 4);
        }
        
        [kpiLab2 setBackgroundColor:[UIColor clearColor]];
        [kpiLab2 setText:[NSString stringWithFormat:@"D: %.2f",_lasetD]];
        [kpiLab2 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab2 setTextColor:fontColor];
        kpiLab2.textAlignment = NSTextAlignmentLeft;

        [pointLab2 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];
        if([AFSetting QUARTZ_KLINE_W] < 270){
            kpiLab3.frame = CGRectMake(177, 0, 75, 17);
            [kpiLab3 setFont:[UIFont systemFontOfSize:10]];
            kpiLab3.adjustsFontSizeToFitWidth = YES;
            pointLab3.frame = CGRectMake(172, 6, 4, 4);
        }else{
            kpiLab3.frame = CGRectMake(205, 0, 95, 17);
            [kpiLab3 setFont:[UIFont systemFontOfSize:11]];
            pointLab3.frame = CGRectMake(200, 6, 4, 4);

        }
        
        [kpiLab3 setBackgroundColor:[UIColor clearColor]];
        [kpiLab3 setText:[NSString stringWithFormat:@"J: %.2f",_lasetJ]];
        
        [kpiLab3 setTextColor:fontColor];
        kpiLab3.textAlignment = NSTextAlignmentLeft;
        [pointLab3 setBackgroundColor:[AFUtils UIColorFromRGB:0xf847bd alpha:1.0]];

        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        [pointLab2.layer setMasksToBounds:YES];
        [pointLab2.layer setCornerRadius:2.0];
        [pointLab3.layer setMasksToBounds:YES];
        [pointLab3.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
         [self.kpiInfoView addSubview:pointLab1];
        [self.kpiInfoView addSubview:kpiLab2];
         [self.kpiInfoView addSubview:pointLab2];
        [self.kpiInfoView addSubview:kpiLab3];
         [self.kpiInfoView addSubview:pointLab3];
        

    }
    // 5: OBV能量潮
    else if(kpiType == 5){        
        float _obv = [self.kpi obvLast];

        kpiLab1.frame = CGRectMake(5, 0, 155, 17);
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"OBV: %.2f",_obv]];
        [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0xFDA01C alpha:1.0]];
        
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
    }
    // 6: CCI顺势
    else if(kpiType == 6){ 
        float _cci = [self.kpi cciLast];
        
        kpiLab1.frame = CGRectMake(5, 0, 155, 17);
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"CCI: %.2f",_cci]];
        [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
        

    }
    // 7: PSY心里线
    else if(kpiType == 7){
        float _psy = [self.kpi psyLast];
        
        kpiLab1.frame = CGRectMake(5, 0, 155, 17);
        [kpiLab1 setBackgroundColor:[UIColor clearColor]];
        [kpiLab1 setText:[NSString stringWithFormat:@"PSY: %.2f",_psy]];
        [kpiLab1 setFont:[UIFont systemFontOfSize:11]];
        [kpiLab1 setTextColor:fontColor];
        kpiLab1.textAlignment = NSTextAlignmentLeft;
        
        pointLab1.frame = CGRectMake(0, 6, 4, 4);
        [pointLab1 setBackgroundColor:[AFUtils UIColorFromRGB:0x05bbf2 alpha:1.0]];
        [pointLab1.layer setMasksToBounds:YES];
        [pointLab1.layer setCornerRadius:2.0];
        
        [self.kpiInfoView addSubview:kpiLab1];
        [self.kpiInfoView addSubview:pointLab1];
        

    }
}

// 手指 移动时 刷新数据
-(void) kpiDatarefreshWithKpiType:(int)kpiType kipA:(float)kipA kipB:(float)kipB kipC:(float)kipC {
    // 0: MACD平滑异同平均线
    // 1: VOL成交量
    // 2: RSI强弱指标
    // 3: BOLL布林线
    // 4: KDJ随机指标
    // 5: OBV能量潮
    // 6: CCI顺势
    // 7: PSY心里线
    if(kpiType == 0){  // MACD
        [kpiLab1 setText:[NSString stringWithFormat:@"DIF : %0.4f",kipA]];
        [kpiLab2 setText:[NSString stringWithFormat:@"DEA : %0.4f",kipB]];
        [kpiLab3 setText:[NSString stringWithFormat:@"MACD : %0.4f",kipC]];
    }
    // VOL
    else if(kpiType == 1){
        float _v = kipA/10000;
        if (_v > 1.0) {
            [kpiLab1 setText:[NSString stringWithFormat:@"VOL : %.0f万",_v]];
        } else {
            [kpiLab1 setText:[NSString stringWithFormat:@"VOL : %.2f万",_v]];
        }

        float _m5 = kipB/10000;
        if (_m5 > 1.0) {
            [kpiLab2 setText:[NSString stringWithFormat:@"5 : %.0f万",_m5]];
        } else {
            [kpiLab2 setText:[NSString stringWithFormat:@"5 : %.2f万",_m5]];
        }

        float _m10 = kipC/10000;
        if (_m10 > 1.0) {
            [kpiLab3 setText:[NSString stringWithFormat:@"10 : %.0f万",_m10]];
        } else {
            [kpiLab3 setText:[NSString stringWithFormat:@"10 : %.2f万",_m10]];
        }
    }
    // RSI
    else if(kpiType == 2){  // RSI
        [kpiLab1 setText:[NSString stringWithFormat:@"6 : %.2f",kipA]];
        [kpiLab2 setText:[NSString stringWithFormat:@"12 : %.2f",kipB]];
    }
    // BOLL
    else if(kpiType == 3){  // 3: BOLL布林线
        [kpiLab1 setText:[NSString stringWithFormat:@"MID : %0.0f",kipA]];
        [kpiLab2 setText:[NSString stringWithFormat:@"UPPER : %0.0f",kipB]];
        [kpiLab3 setText:[NSString stringWithFormat:@"LOWER : %0.0f",kipC]];
    }
    // 4: KDJ随机指标
    else if(kpiType == 4){
        [kpiLab1 setText:[NSString stringWithFormat:@"K : %.2f",kipA]];
        [kpiLab2 setText:[NSString stringWithFormat:@"D : %.2f",kipB]];
        [kpiLab3 setText:[NSString stringWithFormat:@"J : %.2f",kipC]];
    }
    // 5: OBV能量潮
    else if(kpiType == 5){
        [kpiLab1 setText:[NSString stringWithFormat:@"OBV : %.2f",kipA]];
    }
    // 6: CCI顺势
    else if(kpiType == 6){
        [kpiLab1 setText:[NSString stringWithFormat:@"CCI : %.2f",kipA]];
    }
    // 7: PSY心里线
    else if(kpiType == 7){
        [kpiLab1 setText:[NSString stringWithFormat:@"PSY : %.2f",kipA]];
    }
}

@end
